package com.hkd.product_service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkd.common.api.ApiResponse;
import com.hkd.product_service.api.ProductSpecsService;
import com.hkd.product_service.api.dto.InsertBrandDTO;
import com.hkd.product_service.api.dto.InsertProductDTO;
import com.hkd.product_service.api.dto.ProductSpecsDTO;
import com.hkd.product_service.api.entity.BrandInfo;
import com.hkd.product_service.api.entity.ProductSpecs;
import com.hkd.product_service.mapper.ProductInfoMapper;
import com.hkd.product_service.api.ProductInfoService;
import com.hkd.product_service.api.entity.ProductInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author HKD
 * @date 2022/9/17 21:50
 */
@RestController
public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, ProductInfo> implements ProductInfoService {
    @Autowired
    private ProductSpecsService productSpecsService;

    @Override
    @Transactional
    public ApiResponse insertProduct(String curUserId, InsertProductDTO param) {
        //商品入库
        ProductInfo productInfo = BeanUtil.copyProperties(param, ProductInfo.class);
        productInfo.setCreateId(curUserId);
        productInfo.setUpdateId(curUserId);
        this.save(productInfo);
        String productInfoId = productInfo.getId();
        //规格入库
        List<ProductSpecsDTO> specsList = param.getProduceSpecsList();
        List<ProductSpecs> specs=new ArrayList<>();
        specsList.stream().forEach(item->{
            ProductSpecs productSpecs = BeanUtil.copyProperties(item, ProductSpecs.class);
            productSpecs.setProductId(productInfoId);
            specs.add(productSpecs);
        });
        productSpecsService.specsSaveBatch(specs);
        return ApiResponse.ok();
    }

}
